<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="模块" prop="moduleId">
      <el-select v-model="dataForm.moduleId" filterable placeholder="请选择模块">
        <el-option
          v-for="item in moduleList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="排序倒序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="排序倒序"></el-input>
    </el-form-item>
    <el-form-item label="激活状态" prop="activeStatus">
      <el-radio-group v-model="dataForm.activeStatus">
        <el-radio :label="0">已激活</el-radio>
        <el-radio :label="-1">待激活</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          moduleId: '',
          orderNum: '',
          activeStatus: 0,
        },
        dataRule: {
          moduleId: [
            { required: true, message: '模块ID不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '排序倒序不能为空', trigger: 'blur' }
          ],
          activeStatus: [
            { required: true, message: '激活状态不能为空', trigger: 'blur' }
          ],
        },

        moduleList:[]

      }
    },
    methods: {
      init (id) {
        this.initModuleList()

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mh/index/module/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.moduleId = data.data.moduleId
                this.dataForm.orderNum = data.data.orderNum
                this.dataForm.activeStatus = data.data.activeStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mh/index/module/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'moduleId': this.dataForm.moduleId,
                'orderNum': this.dataForm.orderNum,
                'activeStatus': this.dataForm.activeStatus,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },

      initModuleList(){
        this.$http({
          url: this.$http.adornUrl(`/mh/module/list`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.moduleList = data.data
          }
        })
      }

    }
  }
</script>
